package com.andova.lifecycle;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import java.util.List;

import androidx.annotation.NonNull;

/**
 * Created by Administrator on 2018-03-21.
 *
 * @author kzaxil
 * @since 1.0.0
 */
class ActivityFactory2 implements LayoutInflater.Factory2 {
    private Activity mActivity;
    private List<IDelegate> mDelegateList;

    ActivityFactory2(Activity activity, @NonNull List<IDelegate> delegates) {
        mActivity = activity;
        this.mDelegateList = delegates;
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        for (IDelegate delegate : mDelegateList) {
            View view = delegate.onCreateView(parent, name, context, attrs);
            if (view != null) return view;
        }
        return mActivity.onCreateView(parent, name, context, attrs);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return onCreateView(null, name, context, attrs);
    }

    List<IDelegate> delegate() {
        return mDelegateList;
    }
}
